package com.example.curriculum.service;

import com.example.curriculum.pojo.Business;
import com.example.curriculum.pojo.InBound;
import com.example.curriculum.pojo.OutBound;
import com.example.curriculum.repository.BusinessRepository;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.List;


@Service
public class BusinessService implements IBusinessService {
    @Resource
    private BusinessRepository brepo;


    @Override
    public Business add(Business business) {
        brepo.save(business);
        Calculate(business.getUser_id());
        return brepo.findByBizId(business.getBiz_id());
    }

    @Override
    public Business select(String biz_id) {
        return brepo.findByBizId(biz_id);
    }

    @Override
    public Business update(Business business) {
        brepo.update(business);
        Calculate(business.getUser_id());
        return brepo.findByBizId(business.getBiz_id());
    }

    @Override
    public void delete(String biz_id) {
        brepo.deleteById(biz_id);
    }
    @Override
    public void Calculate(String user){
        // 当前总的入库吨数
        Double sum = 0.0;
        // 总入库吨量
        Double suminbound = brepo.SumInBound();
        List<OutBound> AllOut = brepo.AllOutBound(user);
        List<InBound> AllIn = brepo.AllInBound(user);
        for(OutBound outbound : AllOut){
            Double sumoutbound = sum;
            Double fee = 0.0;
            LocalDate newtime = outbound.getTime_out();
            Double newweight = outbound.getWeight_out();
            for(InBound pos : AllIn){
                Double weight = pos.getWeight_in(); // 当前入库的吨数
                LocalDate time = pos.getTime_in(); // 当前入库时间
                long cnt = ChronoUnit.DAYS.between(time, newtime); // 差值
                if(cnt < 0){
                    System.out.println("库存不足！");
                    return;
                }
                if(sumoutbound > 0){
                    if(sumoutbound > weight){
                        sumoutbound -= weight;
                    }
                    else{
                        weight -= sumoutbound;
                        sumoutbound = 0.0;
                    }
                }
                if(sumoutbound == 0.0) {
                    long daysBetween = Math.max(cnt - 13, 0); // 时间差
                    if(newweight > weight){
                        newweight -= weight;
                        fee += 0.5 * weight * daysBetween;
                    }
                    else{
                        fee += 0.5 * newweight * daysBetween;
                        newweight = 0.0;
                    }
                }
                if(newweight == 0)
                    break;
            }

            if(newweight > 0){
                System.out.println("库存不足！");
                return;
            }
            Double storage_ton = suminbound - sumoutbound - outbound.getWeight_out();
            sum += outbound.getWeight_out();
            Business business = brepo.findByBillId(outbound.getBill_id());
            if(business != null){
                business.setFee(fee);
                business.setStorage_ton(storage_ton);
                brepo.update(business);
            }
        }
    }
}
